package com.jfl.web.controller.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.jfl.annotation.Log;
import com.jfl.base.AbstractController;
import com.jfl.common.Constants;
import com.jfl.core.utils.ShiroUtils;
import com.jfl.enums.LogType;
import com.jfl.enums.Module;
import com.jfl.exception.BusinessException;
import com.jfl.model.SysRole;
import com.jfl.system.SysRoleService;
import com.jfl.utils.ConvertRequestUtil;

/**
 * 角色管理
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月11日]
 * @since V1.00
 */
@Controller
@RequestMapping("${jfl.module.system}/role")
public class SysRoleController extends AbstractController
{
    @Reference(version = "${jfl.version}")
    private SysRoleService sysRoleService;
    
    @Value("${jfl.demoMode}")
    private boolean mode;
    /**
     * 跳转角色管理界面
     * 
     * @return
     */
    @RequiresPermissions("system:role:view")
    @GetMapping
    public String role(ModelMap modelMap)
    {
        return Constants.MODULE_SYS_ROLE_PREFIX + "role";
    }
    
    /**
     * 分页查询
     * 
     * @return
     */
    @RequiresPermissions("system:role:list")
    @PostMapping("/list")
    @ResponseBody
    public ResponseEntity<ModelMap> list(SysRole sysRole)
    {
        PageInfo<SysRole> pageInfo = this.sysRoleService.selectList(ConvertRequestUtil.pageRequest(), sysRole);
        return setSuccessModelMap(pageInfo);
    }
    
    /**
     * 跳转角色新增页面
     * 
     * @param sysRole
     * @return
     */
    @GetMapping("/add")
    public String add(SysRole sysRole)
    {
        return Constants.MODULE_SYS_ROLE_PREFIX  + "add";
    }
    
    /**
     * 跳转角色管理界面
     * 
     * @return
     */
    @Log(module = Module.ROLE, value="新增角色", type=LogType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public ResponseEntity<ModelMap> addSave(SysRole sysRole)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        sysRole.setCreateBy(ShiroUtils.getCurrentUser().getId());
        this.sysRoleService.addRole(sysRole);
        ShiroUtils.clearCachedAuthorizationInfo();
        return setSuccessModelMap();
    }
    
    /**
     * 删除
     * 
     * @param roleId
     * @return
     */
    @Log(module = Module.ROLE, value = "删除角色", type = LogType.DELETE)
    @RequiresPermissions("system:role:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResponseEntity<ModelMap> remove(String ids)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysRoleService.deleteByIds(ids);
        ShiroUtils.clearCachedAuthorizationInfo();
        return setSuccessModelMap();
    }
    
    /**
     * 修改
     * 
     * @param roleId
     * @return
     */
    @GetMapping("edit/{roleId}")
    public String edit(@PathVariable("roleId") Long roleId, ModelMap modelMap)
    {
        modelMap.put("role", this.sysRoleService.queryById(roleId));
        return "system/role/" + "edit";
    }
    
    @Log(module = Module.ROLE, value = "修改角色", type = LogType.UPDATE)
    @RequiresPermissions("system:role:edit")
    @PostMapping("/edit")
    @ResponseBody
    public ResponseEntity<ModelMap> edit(SysRole role)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysRoleService.updateRole(role);
        ShiroUtils.clearCachedAuthorizationInfo();
        return setSuccessModelMap();
    }
    
    /** 
     * 校验角色名称是否唯一
     * @param role
     * @return
     */
    @PostMapping("/checkRoleNameUnique")
    @ResponseBody
    public String checkRoleNameUnique(SysRole role)
    {
        return this.sysRoleService.checkRoleNameUnique(role);
    }
    
    /**
     * 新增数据权限
     */
    @GetMapping("/rule/{roleId}")
    public String rule(@PathVariable("roleId") Long roleId, ModelMap mmap)
    {
        mmap.put("role", this.sysRoleService.queryById(roleId));
        return Constants.MODULE_SYS_ROLE_PREFIX  + "rule";
    }
}
